﻿using Himp.Outpatient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.Outpatient
{
    /// <summary>
    /// 发票使用记录
    /// </summary>
    public class InvoUsedRecService : OutpatientAppService, IInvoUsedRecService
    {
        private IRepository<InvoUsedRec, Guid> _invoUsedRecRepository;

        public InvoUsedRecService(IRepository<InvoUsedRec, Guid> invoUsedRecRepository)
        {
            _invoUsedRecRepository = invoUsedRecRepository;
        }

        /// <summary>
        /// 提交发票
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<InvoUsedRecDto> PostAsync(InvoUsedRecCreateDto input)
        {
            InvoUsedRec invoUsedRec = new InvoUsedRec(input.OrdNo, input.OrdBizSouc);
            invoUsedRec = await _invoUsedRecRepository.InsertAsync(invoUsedRec);
            var itemDto = ObjectMapper.Map<InvoUsedRec, InvoUsedRecDto>(invoUsedRec);

            return itemDto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<InvoUsedRecDto>> GetListAsync(InvoUsedRecInput input)
        {
            var iQuery = await _invoUsedRecRepository.GetQueryableAsync();

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<InvoUsedRec>, List<InvoUsedRecDto>>(iQuery.ToList());

                return new PagedResultDto<InvoUsedRecDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<InvoUsedRec>, List<InvoUsedRecDto>>(items.ToList());

                return new PagedResultDto<InvoUsedRecDto>(totalCount, itemsDto);
            }
        }
    }
}
